package com.dormitory.management.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dormitory.management.system.entity.HousemasterExcel;
import com.dormitory.management.system.entity.StudentExcel;
import com.dormitory.management.system.req.HousemasterPageReq;
import com.dormitory.management.system.req.HousemasterSaveReq;
import com.dormitory.management.system.req.HousemasterUpdateReq;
import com.dormitory.management.system.req.HousemasterUpdateRoomReq;
import com.dormitory.management.system.resp.HousemasterResp;
import com.dormitory.management.system.service.HousemasterService;

import com.dormitory.management.system.utils.EmailUtil;
import com.dormitory.management.system.utils.ExcelUtil;
import com.dormitory.managemnet.util.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 宿管表 前端控制器
 * </p>
 *
 * @author
 * @since 2021-12-17
 */
@Api(tags = "宿管信息管理")
@RestController
@RequestMapping("/housemaster")
public class HousemasterController {
    @Autowired
    private HousemasterService housemasterService;

    @ApiOperation("添加宿管基本信息")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<String> save(@RequestBody HousemasterSaveReq req){
        if(!housemasterService.save(req)) {
            return Result.error("添加失败！");
        }
        return Result.success("添加成功！");
    }

    @ApiOperation("删除宿管")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public Result<String> delete(@PathVariable Long id){
        if(!housemasterService.delete(id)) {
            return Result.error("删除失败！");
        }
        return Result.success("删除成功！");
    }

    @ApiOperation("更新宿管基本信息")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result<String> update(@RequestBody HousemasterUpdateReq req){
        if(!housemasterService.update(req)) {
            return Result.error("更新失败！");
        }
        return Result.success("更新成功！");
    }

    @ApiOperation("安排宿管所属楼栋和房间")
    @RequestMapping(value = "/saveRoom",method = RequestMethod.POST)
    public Result<String> saveRoom(@RequestBody HousemasterUpdateRoomReq req){
        if(!housemasterService.saveRoom(req)) {
            return Result.error("安排失败！");
        }
        return Result.success("安排成功！");
    }

    @ApiOperation("更新宿管所属楼栋和房间")
    @RequestMapping(value = "/updateRoom",method = RequestMethod.POST)
    public Result<String> updateRoom(@RequestBody HousemasterUpdateRoomReq req){
        if(!housemasterService.updateRoom(req)) {
            return Result.error("更新失败！");
        }
        return Result.success("更新成功！");
    }

    @ApiOperation("宿管分页列表")
    @RequestMapping(value = "/getPage",method = RequestMethod.POST)
    public Result<IPage<HousemasterResp>> getPage(@RequestBody HousemasterPageReq req){
        return Result.ok(housemasterService.getPage(req));
    }

    @ApiOperation("Excel导入宿管")
    @PostMapping("/createByExcel")
    @ResponseBody
    public Result createHousemaster(String username, MultipartFile file)throws IOException {

        return housemasterService.importHousemasterByExcel(username,file);
    }

    @ApiOperation("Excel模板下载")
    @GetMapping("/excel/template")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "导入宿管模板";
        String sheetName = "导入宿管模板";
        List<HousemasterExcel> housemasterExcelList = new ArrayList<>();
        housemasterExcelList.add(new HousemasterExcel());
        try {
            ExcelUtil.writeExcel(response, housemasterExcelList, fileName, sheetName, HousemasterExcel.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
